class ShopsController < ApplicationController
  include ControllerHelper
  before_filter :init, :only => :index
  before_filter :require_admin, :except => :index

  def index
    if params[:desire_id] == "all"
      @shops = Shop.joins(:desires,:town).where("shops.status = 'approved' and shops.time IN (?) and towns.name=?",
        ["round", @time], @town).uniq
    else
      @shops = Shop.joins(:desires,:town).where("shops.status = 'approved' and shops.time IN (?) and desires.id = ? and towns.keyword=?",
        ["round", @time], params[:desire_id], @town).uniq
    end
    @desire = Desire.where("status = 'approved' and id = ?", params[:desire_id]).first
  end

  def new
    set_form_settings
    @page["name"] = "shop"
    @shop = Shop.new
    render :layout => "form"
  end

  def create
    begin
      @shop = Shop.new(params[:shop])
      if @shop.save
        redirect_to shop_admin_path(:status => "all") and return
      else
        set_form_settings
        @page["name"] = "shop"
        render :action => "new", :layout => "form"
      end
    rescue
      flash[:notice] = I18n.t "site.message.corrupted_html"
      set_form_settings
      @page["name"] = "shop"
      render :action => "new", :layout => "form"
    end
  end

  def admin
    per_page = params[:per_page] ||= @site["per_page"]["admin"]["shops"]
    conditions = []
    conditions = merge_conditions(conditions, ["status = ?", "approved"]) if params[:status] == "approved"
    conditions = merge_conditions(conditions, ["status <> ?", "approved"]) if params[:status] == "banned"
    @shops = Shop.find(:all, :conditions => conditions).uniq.paginate(:page => params[:page], :per_page => per_page)
    set_admin_settings
    render :layout => "cms"
  end

  def destroy
    @shop = Shop.find(:first, :conditions => ["id = ?", params[:id]])
    if @shop
      @shop.destroy
    else
      flash[:notice] = I18n.t "site.message.no_shop"
    end
    redirect_to shop_admin_path(:status => "all")
  end

  def edit
    set_form_settings
    @page["name"] = "shop"
    @shop = Shop.find(:first, :conditions => ["id = ?", params[:id]])
    if @shop.nil?
      flash[:notice] = I18n.t "site.message.no_shop"
      redirect_to shop_admin_path(:status => "all") and return
    else
      render :layout => "form"
    end
  end

  def update
    @shop = Shop.find(:first, :conditions => ["id = ?", params[:id]])
    if @shop.nil?
      flash[:notice] = I18n.t "site.message.no_shop"
      redirect_to shop_admin_path(:status => "all") and return
    else
      if @shop.update_attributes(params[:shop])
        redirect_to shop_admin_path(:status => "all") and return
      else
        set_form_settings
        @page["name"] = "shop"
        render :layout => "form", :action => "edit", :id => params[:id]
      end
    end
  end

  def approve_ban
    shop = Shop.find(params[:id])
    old_status = shop.status == "approved" ? "approved" : "banned"
    shop.status = shop.status == "approved" ? "banned" : "approved"
    shop.save
    redirect_to shop_admin_path(:status => old_status)
  end

end
